package com.tbs.easyhomemoneyserver.dao

import com.tbs.easyhomemoneyserver.DO.UserRights
import org.apache.ibatis.annotations.Mapper
import org.apache.ibatis.annotations.Select
import tk.mybatis.mapper.common.BaseMapper

@Mapper
interface UserRightsMapper : BaseMapper<UserRights> {
    /**
     * 查询指定用户指定权限，若id为空则代表该rights_id的权限不存在
     *
     * @param role
     * @param user
     * @return
     */
    @Select(
        """select *
from (select distinct ur.*
      from user_rights ur
               left join api_rights ar on ar.role =  #{role}
      where ur.user_id = #{user}
        and ar.id = ur.rights_id) as t1
union all
(select null                as id,
        0                   as delete_mark,
        ar2.id                 rights_id,
        CURRENT_TIMESTAMP() as update_time,
        null                as user_id
 from api_rights ar2
          left join user_rights u on u.user_id = #{user} and u.rights_id = ar2.id
 where ar2.role = #{role} and u.id is null);
"""
    )
    fun findByRoleAndUser(role: String, user: Long): List<UserRights>
}
